/***************************************************************************
 *   Copyright (C) 2008 by Andreas Theofilu                                *
 *   andreas@theosys.at                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation version 3 of the License.                *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "settingswidget.h"
#include <KConfig>
#include <knuminput.h>
#include <kcombobox.h>
#include <kurlrequester.h>
#include <klineedit.h>
#include <qdir.h>
#include <qcheckbox.h>
#include <qradiobutton.h>

settingsWidget::settingsWidget ( QWidget* parent, Qt::WFlags fl )
		: QDialog(parent, fl), Ui::settingsWidgetBase ()
{
KComboBox *cb;
QDir dir = QDir::home();
QString path = dir.absolutePath();

	setupUi (this);
	// Load the config parameters
	cbDevice->setMode(KFile::ExistingOnly | KFile::LocalOnly);
	cb = cbDevice->comboBox();
	cb->insertUrl(1, KUrl("/dev/ttyUSB0"));
	cb->insertUrl(2, KUrl("/dev/ttyUSB1"));
	cb->insertUrl(3, KUrl("/dev/ttyS0"));
	cb->insertUrl(4, KUrl("/dev/ttyS1"));
	KConfig cfg (QString("sportwatcher.rc"), KConfig::SimpleConfig);
	KConfigGroup ic (&cfg, "SportWatcher");
	intLower1->setValue(ic.readEntry("lower1", 0));
	intLower2->setValue(ic.readEntry("lower2", 0));
	intLower3->setValue(ic.readEntry("lower3", 0));
	intUpper1->setValue(ic.readEntry("upper1", 0));
	intUpper2->setValue(ic.readEntry("upper2", 0));
	intUpper3->setValue(ic.readEntry("upper3", 0));
	intMaxHr->setValue(ic.readEntry("maxHr", 0));
	intRestHr->setValue(ic.readEntry("restHr", 0));
	intVO2max->setValue(ic.readEntry("vo2max", 0));
	intWeight->setValue(ic.readEntry("weight", 0));
	cbSampleTime->setCurrentIndex(ic.readEntry("seconds", 1));
	// If serial device is selected, activate the radio buttons
	// to select the type of Forerunner used.
	cbSerial->setChecked(ic.readEntry("Serial", false));

	if (ic.readEntry("Serial", false))
	{
	   rbFr50->setEnabled (true);
//	   rbFr405->setEnabled (true);
	   cbDevice->setEnabled (true);

	   // If "Forerunner" == false, Forerunner 405 is set.
	   if (ic.readEntry("Forerunner", false))
	   {
	      rbFr50->setChecked(false);
	      rbFr405->setChecked(true);
	   }
	   else
	   {
	      rbFr50->setChecked(true);
	      rbFr405->setChecked(false);
	   }
	}
	else
	{
	   rbFr50->setEnabled (false);
	   rbFr405->setEnabled (false);
	   cbDevice->setEnabled (false);
	}

	cbContour->setChecked(ic.readEntry("Contour", false));
	cbDevice->setUrl(ic.readEntry("Device", QString("/dev/ttyUSB0")));
	lnData->setUrl(ic.readEntry("Data", path + "/.sportwatcher"));
	lnHRM->setUrl(ic.readEntry("HRM", path + "/polar"));
	lnMapFile->setUrl(ic.readEntry("MAP", ic.readEntry("Data", path + "/.sportwatcher/track.wms")));
	cbUnit->setCurrentIndex(ic.readEntry("Units", 0));
	cbMapType->setCurrentIndex(ic.readEntry("MapType", 7));
}

settingsWidget::~settingsWidget()
{
}

/*$SPECIALIZATION$*/
void settingsWidget::cbSerialSlot (bool state)
{
	if (state)
	{
	   rbFr50->setEnabled (true);
//	   rbFr405->setEnabled (true);
	   cbDevice->setEnabled (true);
	}
	else
	{
	   rbFr50->setEnabled (false);
	   rbFr405->setEnabled (false);
	   cbDevice->setEnabled (false);
	}
}

void settingsWidget::rbFr50Slot (bool state)
{
	if (state && rbFr405->isChecked())
	   rbFr405->setChecked(false);
	else
	   rbFr405->setChecked(true);
}

void settingsWidget::rbFr405Slot (bool state)
{
	if (state && rbFr50->isChecked())
	   rbFr50->setChecked(false);
	else
	   rbFr50->setChecked(true);
}

void settingsWidget::btCancelSlot()
{
	done(QDialog::Rejected);
}

void settingsWidget::btSaveSlot()
{
	KConfig cfg (QString("sportwatcher.rc"), KConfig::SimpleConfig);
	KConfigGroup ic (&cfg, "SportWatcher");
	// Put the data into the config file
	ic.writeEntry("lower1", intLower1->value());
	ic.writeEntry("lower2", intLower2->value());
	ic.writeEntry("lower3", intLower3->value());
	ic.writeEntry("upper1", intUpper1->value());
	ic.writeEntry("upper2", intUpper2->value());
	ic.writeEntry("upper3", intUpper3->value());
	ic.writeEntry("maxHr", intMaxHr->value());
	ic.writeEntry("restHr", intRestHr->value());
	ic.writeEntry("vo2max", intVO2max->value());
	ic.writeEntry("weight", intWeight->value());
	ic.writeEntry("seconds", cbSampleTime->currentIndex());
	ic.writeEntry("Serial", cbSerial->isChecked());
	ic.writeEntry("Contour", cbContour->isChecked());
	ic.writeEntry("Device", cbDevice->url());
	ic.writeEntry("Data", lnData->lineEdit()->text());
	ic.writeEntry("HRM", lnHRM->lineEdit()->text());
	ic.writeEntry("MAP", lnMapFile->lineEdit()->text());
	ic.writeEntry("Units", cbUnit->currentIndex());
	ic.writeEntry("MapType", cbMapType->currentIndex());
	ic.writeEntry("Forerunner", rbFr50->isChecked());
	cfg.sync();
	done(QDialog::Accepted);
}



#include "settingswidget.moc"

